package org.mineground.commands.irc;

import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.jibble.pircbot.Colors;
import org.jibble.pircbot.User;
import org.mineground.Main;
import org.mineground.Utilities;
import org.mineground.handlers.irc.CommandExecutor;
import org.mineground.handlers.irc.UserLevel;
import org.mineground.player.MinegroundPlayer;

/**
 * @name Mute.java
 * @author Daniel Koenen (2012)
 */
public class Mute implements CommandExecutor {
    @Override
    public void onCommand(User sender, UserLevel level, String channel, String command, String[] arguments) {
        if (level.compareTo(UserLevel.IRC_OP) < 0) {
	    return;
        }
        
        if (arguments.length == 0) {
            Main.getInstance().getIRCHandler().sendMessage(channel, Colors.RED + "* Usage:" + Colors.NORMAL + " !mute [player] ([time in minutes])");
	    return;
	}
        
        Player mutePlayer = Main.getInstance().getServer().getPlayer(arguments[0]);
        
        if (mutePlayer == null) {
            Main.getInstance().getIRCHandler().sendMessage(channel, Colors.RED + "* Error: Invalid player.");
	    return;
	}
        
        int muteTime = -1;
        
        if (arguments.length > 1 && Utilities.isNumeric(arguments[1])) {
            muteTime = Integer.parseInt(arguments[1]);
        }
        
        if (muteTime == 0) {
            Main.getInstance().getIRCHandler().sendMessage(channel, Colors.RED + "* Error: Invalid time value.");
	    return;
	}
        
        MinegroundPlayer playerInstance = Main.getInstance().getPlayer(mutePlayer);
        playerInstance.mutePlayer(muteTime);
        
        Main.getInstance().getIRCHandler().sendMessage(channel, Colors.DARK_GREEN + Utilities.fixName(mutePlayer) + Colors.DARK_GREEN + " has been muted " + ((muteTime > 0) ? ("for " + muteTime + " minutes.") : ("permanently.")));
        mutePlayer.sendMessage(ChatColor.RED + "You have been muted " + ((muteTime > 0) ? ("for " + muteTime + " minutes.") : ("permanently.")));
    }
}

